package com.ddxz.elastic.config;

import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2021/2/23
 */

@Configuration
public class ElasticZkConfig {
    @Value("${ddxz.elastic.zk.address:127.0.0.1:2181}")
    private String serList;

    @Value("${ddxz.elastic.zk.namespace:ddxz}")
    private String namespace;

    @Bean
    public ZookeeperRegistryCenter zkRegCenter() {
        ZookeeperConfiguration configuration = new ZookeeperConfiguration(serList, namespace);
        ZookeeperRegistryCenter registryCenter = new ZookeeperRegistryCenter(configuration);
        // init方法调用之后才会创建zk的客户端，否则会抛出空指针异常
        registryCenter.init();
        return registryCenter;
    }

    @Bean
    public JobEventConfiguration jobEventConfiguration(DataSource dataSource) {
        return new JobEventRdbConfiguration(dataSource);
    }
}
